package com.lx.demo;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author c先生
 * @date 2022年03月29日 下午 8:32:35
 * @describe

 *      */

public class EmpManager {
    ArrayList<Dept> arrayList = new ArrayList();
    //List<Emp> list = new ArrayList<>();
    public EmpManager() {
        this.init();
    }

    //删除重名员工
    public boolean deleteEmp2(int empNum) {
        for (Dept dept : arrayList) {
            for (Emp emp : dept.getEmps()) {
                if (emp.getEmpNum() == empNum) {
                    dept.getEmps().remove(emp);
                    return true;
                }
            }
        }
        return false;
    }

    //删除不重名员工
    public boolean deleteEmp(String empName) {
        for (Dept dept : arrayList) {
            for (Emp emp : dept.getEmps()) {
                if (emp.getEmpName().equals(empName)) {
                    dept.getEmps().remove(emp);
                    return true;
                }
            }
        }
        return false;
    }

    //展示同名员工
    public  void  showEmp(String empName){
        for (Dept dept : arrayList) {
            for (Emp emp : dept.getEmps()) {
                if(emp.getEmpName().equals(empName)) {
                    System.out.println(emp.toString());
                }
            }
        }
    }

    //计算同名员工
    public int countEmp(String empName) {
        int count = 0;
        for (Dept dept : arrayList) {
            for (Emp emp : dept.getEmps()) {
                if (emp.getEmpName().equals(empName)) {
                    count++;
                }
            }
        }
        return count;
    }
    //获取所有部门名称
    public String getAllDeptName(){
        String allDeptName = "";
        Iterator it = arrayList.iterator();
        while (it.hasNext()){
            Dept dept3 = (Dept) it.next();
            allDeptName +=  dept3.getDeptName()+" ";
        }
        return allDeptName;
    }

    //输入一个年份，给满足此年份员工涨2000工资
    public boolean increaseSalary(int year){
        Iterator it = arrayList.iterator();
        while (it.hasNext()){
            Dept dept = (Dept) it.next();
            if(dept.getEmps() != null ){
                Iterator it2 = dept.getEmps().iterator();
                Emp emp = (Emp)it2.next();
                if(emp.getEmpHirDate().getYear()==year) {
                    emp.setEmpSalary(emp.getEmpSalary()+2000.00);
                    return true;
                }
            }
        }
        return false;
    }

    //员工转岗
    public boolean EmpChangeStation(String oldDeptName,int empNum,String newDeptName){
        Iterator it = arrayList.iterator();

        //Emp emp = null;
        while (it.hasNext()){
            Dept dept = (Dept) it.next();
            List<Emp> list = dept.getEmps();
            if(dept.getDeptName().equals(oldDeptName)){
                Iterator it2 = dept.getEmps().iterator();
                while (it2.hasNext()){
                    Emp emp = (Emp) it2.next();
                    if(emp.getEmpNum() == empNum){
                        //dept.getEmps().remove(emp);
                        it2.remove();
                    }
                    Iterator it3 = arrayList.iterator();
                    while (it3.hasNext()) {
                        Dept dept1 = (Dept) it3.next();
                        if (dept1.getDeptName().equals(newDeptName)) {
                            if(dept1.getEmps() != null){
                                //dept1.setEmps(new ArrayList<Emp>());
                                dept1.getEmps().add(emp);
                                return true;
                            }


                        }
                    }
                }
            }
        }
        return false;
    }

    //新增一个员工
    public boolean addEmp(String deptName1,Integer empNum,String empName,String empGender,LocalDate empHirDate,Double empSalary,String homeTown,String empLevel){
        Emp emp = new Emp(empNum,empName,empGender,empHirDate,empSalary,homeTown,empLevel);
        Iterator it = arrayList.iterator();
        while (it.hasNext()){
            Dept dept = (Dept) it.next();
            if(dept.getDeptName().equals(deptName1)){
            dept.getEmps().add(emp);
            return true;
            }
        }
        return false;
    }
    //新增一个部门
    public boolean addDept(Integer deptNum,String deptName,String deptDescrib,Office office){
        List<Emp> list = new ArrayList<>();
        Dept dept = new Dept(deptNum,deptName,deptDescrib,office,list);
        boolean flag = arrayList.add(dept);
        if(flag){
            return true;
        };
        return false;
    }

    //根据部门名称查看当前部门门的所有员工信息
    public void showEmpByDept(String deptName){
        Iterator it = arrayList.iterator();
        while (it.hasNext()){
            Dept dept = (Dept) it.next();
            if(deptName.equals(dept.getDeptName())){
                System.out.println("编号"+dept.getDeptNum()+"  部门:"+dept.getDeptName()+"  "+dept.getDeptDescribe()+
                        "  在"+dept.getOffice().getOfficeNum()+"室办公  " +"本部门下所有员工如下: ");
                Iterator it2 = dept.getEmps().iterator();
                while (it2.hasNext()){
                    Emp emp = (Emp) it2.next();
                    System.out.println(emp.toString());
                }
            }
        }
    }

    //展示所有部门以及员工信息
    public void showAll(){
        Iterator it = arrayList.iterator();
        while (it.hasNext()){
            Dept dept = (Dept) it.next();
            System.out.println("编号"+dept.getDeptNum()+"  部门:"+dept.getDeptName()+"  "+dept.getDeptDescribe()+
                    "  在"+dept.getOffice().getOfficeNum()+"室办公  " +"本部门下所有员工如下: ");
            if(dept.getEmps() != null ){
                Iterator it2 = dept.getEmps().iterator();
                    while (it2.hasNext()){
                        Emp emp = (Emp) it2.next();
                        System.out.println(emp.toString());
                    }
            }else {
                break;
            }
        }
    }

    //初始化数据
    public void init(){
        Office office1 = new Office(001,"厕所旁边");
        List<Emp> emps1 = new ArrayList<>();
        Emp emp1 = new Emp(1,"张恒","女",LocalDate.of(2022,3,29),500.00,"厕所","A");
        Emp emp2 = new Emp(2,"杨威","男",LocalDate.of(2000,1,1),1000.00,"湖南","B");
        Emp emp3 = new Emp(3,"杨威","男",LocalDate.of(2010,3,15),11000.00,"北京","A");
        emps1.add(emp1);
        emps1.add(emp2);
        emps1.add(emp3);
        Dept dept1 = new Dept(01,"清洁部","掏粪",office1,emps1);

        Office office2 = new Office(002,"大门口");
        List<Emp> emps2 = new ArrayList<>();
        Emp emp11 = new Emp(1,"张二恒","女",LocalDate.of(2022,3,29),500.00,"日本","A");
        Emp emp22 = new Emp(2,"杨二威","男",LocalDate.of(2000,1,1),1000.00,"湖北","B");
        Emp emp33 = new Emp(3,"周煜民","男",LocalDate.of(2010,3,15),11000.00,"东京","A");
        emps2.add(emp11);
        emps2.add(emp22);
        emps2.add(emp33);
        Dept dept2 = new Dept(02,"保卫部","安全保卫",office2,emps2);

        Office office3 = new Office(003,"办公3号楼");
        List<Emp> emps3 = new ArrayList<>();
        Emp emp111 = new Emp(1,"张三恒","女",LocalDate.of(2022,3,29),500.00,"厕所","A");
        Emp emp222 = new Emp(2,"杨三威","男",LocalDate.of(2000,1,1),1000.00,"湖南","B");
        Emp emp333 = new Emp(3,"也应龙","男",LocalDate.of(2010,3,15),11000.00,"北京","A");
        emps3.add(emp111);
        emps3.add(emp222);
        emps3.add(emp333);
        Dept dept3 = new Dept(03,"开发部","写代码",office3,emps3);
        arrayList.add(dept1);
        arrayList.add(dept2);
        arrayList.add(dept3);
    }
}
